<template>
	<!-- <text>{{this.orders.length}}</text> -->
	<view
		v-for="(order,index) in orders"
		:key="order.orderNo"
		@click="onOrderItemClicked(order.orderNo)"
		class="order-container"
		hover-class="order-container-hover">
		
		<!-- 订单信息容器 -->
		<view
			class="row-container">
			<!-- 订单时间 -->
			<text class="order-text">{{order.createTime}}</text>
			<!-- 支付状态 -->
			<text class="order-text">{{order.orderStatus}}</text>
		</view>
		
		<!-- 商品信息容器 -->
		<view
			class="row-container">
			<!-- 商品图片 -->
			<image
				:src="order.productCover"
				mode="aspectFit"
				class="product-cover"></image>
			<!-- 商品信息 -->
			<view class="product-info-container">
				<!-- 商品名称 -->
				<text class="order-text">1 件{{order.productName}}</text>
				<!-- 总价 -->
				<text class="pay-amount">{{order.payAmount}} 元</text>
			</view>
		</view>
		
		<!-- 分隔线 -->
		<view class="divider">
			
		</view>
	</view>
</template>

<script>
	import Order from '../../models/order'
	
	let  tag = "order-list"
	
	export default {
		onLoad() {
			this.$log.info(tag, "onLoad")
			this.fetchOrderList()
		},
		data() {
			return {
				orders: [],
				num: ""
			}
		},
		methods: {
			onOrderItemClicked(orderNo){
				this.$log.info(tag, "onOrderItemClicked");
				uni.navigateTo({
					url: `/pages/order/order?orderNo=${orderNo}`
				})
				
			},
			parseOrderListData(orderJsonArray){
				this.$log.info(tag, `parseOrderListData ${orderJsonArray.length}`);
				let orderList = [];
				for(var i =orderJsonArray.length-1; i>=0; i--){
					let orderJsonObject = orderJsonArray[i];
					orderList.push(
						new Order(
							orderJsonObject.orderNo,
							orderJsonObject.createTime,
							orderJsonObject.orderStatus,
							`${this.constant.BASE_URL}${orderJsonObject.productCover}`,
							orderJsonObject.productName,
							orderJsonObject.payAmount,
						
						)
					);
				}
				
				return orderList;
				
			},
			fetchOrderList(){
				this.$log.info(tag, "fetchOrderList")
				
				uni.request({
					url:`${this.constant.BASE_URL}/api/order/list`,
					method:'GET',
					header:{
						'Authorization': this.loginManager.getToken()
					},
					success: (res) => {
						// this.$log.info(tag, JSON.stringify(res.data))
						console.log(res.data)
						if(res.data.code==200){
							this.orders = this.parseOrderListData(res.data.rows)
							this.$log.info(tag, `fetchOrderList success ${this.orders.length} parsed`)
						}
					
					},
					fail: (err) => {
						this.$log.error(err.errMsg)
					}
				})
			}
		}
	}
</script>

<style>
	.order-container {
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	
	.row-container {
		width: 90%;
		margin-top: 5px;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}
	
	.product-cover {
		width: 70px;
		height: 70px;
	}
	
	.product-info-container {
		display: flex;
		flex-direction: column;
		justify-content: space-evenly;
		align-items: flex-end;
	}
	
	.order-text {
		color: dimgrey;
		font-size: 36rpx;
	}
	
	.pay-amount {
		color: black;
		font-weight: bold;
		font-style: 36rpx;
	}
	
	.divider {
		width: 100%;
		height: 1px;
		background-color: lightgray;
		margin-top: 5px;
		margin-bottom: 5px;
	}
	
	.order-container-hover {
		opacity: 0.5;
	}
</style>
